package com.wecon.dianping.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wecon.dianping.common.AdminPermission;
import com.wecon.dianping.common.BusinessException;
import com.wecon.dianping.common.CommonUtil;
import com.wecon.dianping.common.EmBusinessError;
import com.wecon.dianping.model.Category;
import com.wecon.dianping.model.Shop;
import com.wecon.dianping.request.CategoryCreateReq;
import com.wecon.dianping.request.PageQuery;
import com.wecon.dianping.request.ShopCreateReq;
import com.wecon.dianping.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.controller.admin
 * @description:
 * @date 2020/3/18 10:32
 */
@RestController
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 获取品类列表
     *
     * @return /
     */
    @RequestMapping("/index")
    @AdminPermission
    public ModelAndView index(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<Shop> shopList = shopService.selectAll();
        PageInfo<Shop> shopPageInfo = new PageInfo<>(shopList);

        ModelAndView modelAndView = new ModelAndView("/admin/shop/index.html");
        modelAndView.addObject("data", shopPageInfo);
        modelAndView.addObject("CONTROLLER_NAME", "shop");
        modelAndView.addObject("ACTION_NAME", "index");

        return modelAndView;
    }

    /**
     * 创建页
     *
     * @return /
     */
    @RequestMapping("/createPage")
    @AdminPermission
    public ModelAndView createPage() {
        ModelAndView modelAndView = new ModelAndView("/admin/shop/create.html");
        modelAndView.addObject("CONTROLLER_NAME", "shop");
        modelAndView.addObject("ACTION_NAME", "create");

        return modelAndView;
    }

    /**
     * 创建商品
     *
     * @param shopCreateReq /
     * @param bindingResult     /
     * @param response          /
     * @throws BusinessException /
     * @throws IOException       /
     */
    @RequestMapping(value = "create", method = {RequestMethod.POST})
    @AdminPermission
    public void create(@Valid ShopCreateReq shopCreateReq, BindingResult bindingResult, HttpServletResponse response) throws BusinessException, IOException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }

        Shop shop = new Shop();
        shop.setName(shopCreateReq.getName());
        shop.setPricePerMan(shopCreateReq.getPricePerMan());
        shop.setLatitude(shopCreateReq.getLatitude());
        shop.setLongitude(shopCreateReq.getLongitude());
        shop.setCategoryId(shopCreateReq.getCategoryId());
        shop.setTags(shopCreateReq.getTags());
        shop.setStartTime(shopCreateReq.getStartTime());
        shop.setEndTime(shopCreateReq.getEndTime());
        shop.setAddress(shopCreateReq.getAddress());
        shop.setSellerId(shopCreateReq.getSellerId());
        shop.setIconUrl(shopCreateReq.getIconUrl());
        shopService.create(shop);

        // 页面重定向
        response.sendRedirect("/admin/shop/index");
    }

}
